package render

import (
	"MetroBuilderGameJam/gameEngine/drawer"
	"MetroBuilderGameJam/gameEngine/geom"
	"MetroBuilderGameJam/gameEngine/utils"
	"github.com/hajimehoshi/ebiten/v2"
)

func NewTextOnRectangleRenderer(d *drawer.Drawer, size geom.IntSize) *TextRenderer {
	return &TextRenderer{
		drawer: d,
		size:   size,
	}
}

type TextRenderer struct {
	drawer *drawer.Drawer
	size   geom.IntSize
}

func (b *TextRenderer) Draw(screen *ebiten.Image, text string, p geom.Point) *utils.Figure {
	if p.X < 0 {
		p.X = float64(b.size.Width) + p.X
	}
	if p.Y < 0 {
		p.Y = float64(b.size.Height) + p.Y
	}
	length := float64(len([]rune(text)))
	b.drawer.FillSimpleRectangle(screen, p.AsVec2().Sub(geom.NewVec2(length, 8)).AsPoint(), geom.Size{Width: length * 7, Height: 18}, drawer.ColorBlack)
	b.drawer.Text(screen, text, p, drawer.ColorWhite)
	return utils.NoFigure()
}
